package com.jf.exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jf.exam.mapper.ClassMapper;
import com.jf.exam.mapper.ExamClassMapper;
import com.jf.exam.mapper.TeacherClassMapper;
import com.jf.exam.pojo.data.ClassDO;
import com.jf.exam.pojo.vo.*;
import com.jf.exam.service.ClassService;
import com.jf.exam.utils.PageBean;
import com.jf.exam.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <br/>
 * Created by weidong on 2018/07/18
 */
@Service("classService")
public class ClassServiceImpl implements ClassService {

    private final static Logger LOG = LoggerFactory.getLogger(ClassServiceImpl.class);

    @Resource
    private ClassMapper classMapper;

    @Autowired
    TeacherClassMapper teacherClassMapper;

    //班级和试卷关系的Mapper
    @Autowired
    ExamClassMapper examClassMapper;

    @Override
    public Result addClass(ClassVO classVO) throws Exception {
        if (findGradeByCno(classVO)) {
            return Result.getFailure("此班级已经存在");
        }
        //保存到数据库
        int count = classMapper.addClass(classVO);
        if (count > 0) {
            return new Result(Result.CODE_SUCCESS, "添加班级成功");
        }
        throw new RuntimeException("添加错误");
    }

    @Override
    public Result updateClass(ClassVO classVO) throws Exception {
        return null;
    }

    @Override
    public Result findDetailClass(ClassVO classVO) throws Exception {
        return null;
    }

    @Override
    public PageBean<ClassListVO> listClass(ClassVO query) throws Exception {

        //防止页码越界
        Integer count = classMapper.countClass(query);
        int max = (count + query.getPageSize() - 1) / query.getPageSize();
        //为了考虑 数据库中只有1-5条数据时
        if (query.getPageCode() > max) {
            query.setPageCode(max);
        }

        PageHelper.startPage(query.getPageCode(), query.getPageSize());
        List<ClassListVO> classLists = classMapper.listClazzAssociation(query);

        //插件提供的分页信息
        PageInfo<ClassListVO> info = new PageInfo<>(classLists);

        //根据插件提供的分页信息，构造我们自己的分页信息
        PageBean<ClassListVO> pageBean = new PageBean<ClassListVO>(classLists, info.getPageSize()
                , query.getPageCode(), (int) info.getTotal(), query.getSize());
        System.out.println(classLists);
        return pageBean;
    }

    @Override
    public Result listClassPage(ClassVO classVO) throws Exception {
        return null;
    }

    @Override
    public Result countClass(ClassVO classVO) throws Exception {
        return null;
    }


    /**
     * 根据条件查询班级是否存在
     *
     * @param classVO 班级信息
     * @return true 存在， false 不存在
     */
    public boolean findGradeByCno(ClassVO classVO) {
        List<ClassDO> classDOS = classMapper.listClass(classVO);
        if (classDOS != null && !classDOS.isEmpty()) {
            return true;
        }
        return false;
    }

    @Override
    public Result deleteClass(ClassVO classVO) throws Exception {
        if (!findGradeByCno(classVO)) {
            return Result.getFailure("班级不存在，无法删除");
        }
        classVO.setDelFlag(1);
        Integer count = classMapper.deleteClass(classVO);
        if (count > 0) {
            return new Result(Result.CODE_SUCCESS, "删除班级成功");
        }
        throw new RuntimeException("添加错误");
    }

    @Override
    public List<ClassDO> listClassByGradeAndMajor(ClassVO classVO) {
        return classMapper.listClass(classVO);
    }


    @Override
    public Result listByTeacher(TeacherVO teacherVO) {
        //根据教师id去查询所教的班级
        //拿教师的id去teacher_class表里面查询
        //SELECT * FROM class WHERE id IN(SELECT cid FROM teacher_class WHERE tid = '1111')

        List<ClassDO> classDOS = classMapper.listByTeacher(teacherVO);
        return new Result(Result.CODE_SUCCESS, classDOS);
    }

    @Override
    public Result addExamClass(String examId, List<ExamClassVO> vos) {
        //清空之前的记录
        int count = examClassMapper.deleteExamClassByExamId(examId);
        System.out.println(count);
        for (ExamClassVO vo : vos) {
            examClassMapper.addExamClass(vo);
        }
        return Result.getSuccess("添加成功");
    }

    @Override
    public Result listByExam(ExamVO teacherVO) {
        List<ClassDO> classDOS = classMapper.listByExam(teacherVO);
        return new Result(Result.CODE_SUCCESS, classDOS);
    }


}